package Music_Playing;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Map.Entry;

public class lyric {

	public static Map<Long, String> gainLyricList(Map<String, String> map) {
		if (map != null) {
			Map<Long, String> tmap = new LinkedHashMap<Long, String>();
			for (Entry<String, String> entry : map.entrySet()) {
				tmap.put(timeTransition(entry.getKey()), entry.getValue());
			}
			return tmap;
		}
		return null;
	}

	public static String gainLyric(long time, Map<String, String> tmap) {
		Map<Long, String> map = gainLyricList(tmap);

		String string = null;
		if (map != null) {
			Iterator<Map.Entry<Long, String>> it = map.entrySet().iterator();
			if (it.hasNext()) {
				Map.Entry<Long, String> entry = it.next();
				while (it.hasNext()) {
					if (entry.getKey() <= time) {
						string = entry.getValue();
						if (!it.hasNext()) {
							string = entry.getValue();
							break;
						}
					}
					entry = it.next();
					if (entry.getKey() > time) {
						return string;
					}

				}
			}

		}

		return string;
	}

	public static long timeTransition(String time) {
		long t = -1;
		String[] strings = time.split("[^0-9]");
		if (strings.length == 3) {
			t = (Long.parseLong(strings[0]) * 60 + Long.parseLong(strings[1])) * 1000 + Long.parseLong(strings[2]);

		}
		return t;
	}

}
